package com.itsv.xhyy;

import java.util.ArrayList;

import com.itsv.cyzx.adapter.TWAdapter;
import com.itsv.cyzx.bean.TWBean;
import com.itsv.cyzx.db.DingCanDB;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MyDingCanActivity extends Activity implements OnClickListener {

	private ListView listView;
	private ArrayList<TWBean> twBeans = new ArrayList<TWBean>();
	private ArrayList<TWBean> wwctwBeans = new ArrayList<TWBean>();
	private ArrayList<TWBean> ddtwBeans = new ArrayList<TWBean>();
	private TWAdapter twAdapter;
	private Button backButton, tijiaoButton;
	private DingCanDB db;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_tw_list);
		db = new DingCanDB(MyDingCanActivity.this);
		initView();

	}

	@Override
	protected void onResume() {
		new GetWWCDataTask().execute();
		super.onResume();
	}

	private void initView() {
		backButton = (Button) findViewById(R.id.tw_ly_back);
		tijiaoButton = (Button) findViewById(R.id.tw_ly_tj);
		backButton.setOnClickListener(this);
		tijiaoButton.setOnClickListener(this);
		listView = (ListView) findViewById(R.id.tw_ly_list);
		twAdapter = new TWAdapter(this, twBeans, listView);
		twAdapter.setData(twBeans);
		listView.setAdapter(twAdapter);
	}

	@Override
	protected void onDestroy() {
		db.closeDB();
		super.onDestroy();
	}

	private class GetWWCDataTask extends AsyncTask<Void, Void, Integer> {
		private boolean flag = false;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Integer doInBackground(Void... params) {

			try {
				wwctwBeans = db.getWWClist();
				twBeans = wwctwBeans;
				flag = true;
			} catch (Exception e) {
				e.printStackTrace();
				flag = false;
			}
			return 1;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			if (flag == true) {
				if (twBeans != null && twBeans.size() > 0) {
					twAdapter.setData(twBeans);
					twAdapter.notifyDataSetChanged();
					tijiaoButton.setVisibility(View.VISIBLE);
				} else {
					tijiaoButton.setVisibility(View.GONE);
					new GetDDDataTask().execute();
				}
			}
		}
	}

	private class GetDDDataTask extends AsyncTask<Void, Void, Integer> {
		private boolean flag = false;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Integer doInBackground(Void... params) {

			try {
				ddtwBeans = db.getDDlist();
				twBeans = ddtwBeans;
				flag = true;
			} catch (Exception e) {
				e.printStackTrace();
				flag = false;
			}
			return 1;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			if (flag == true) {
				if (twBeans != null && twBeans.size() > 0) {
					twAdapter.setData(twBeans);
					twAdapter.notifyDataSetChanged();
				}
			}
		}
	}

	private class UpDDDataTask extends AsyncTask<Void, Void, Integer> {
		private boolean flag = false;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Integer doInBackground(Void... params) {

			try {
				for (TWBean twBean : wwctwBeans) {
					db.insertDDList(twBean);
				}
				flag = true;
			} catch (Exception e) {
				e.printStackTrace();
				flag = false;
			}
			return 1;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			if (flag == true) {
				db.deleteWWCData();
				tijiaoButton.setVisibility(View.GONE);
				Toast.makeText(MyDingCanActivity.this, "订单提交成功",
						Toast.LENGTH_LONG).show();
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tw_ly_back:
			MyDingCanActivity.this.finish();
			break;
		case R.id.tw_ly_tj:

			db.deleteDDListData();
			new UpDDDataTask().execute();
			break;
		default:
			break;
		}

	}

}
